package com.kuafood.flutter_kuafood_camera

import android.content.Context
import android.hardware.Camera
import android.util.Log
import android.view.View
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.platform.PlatformView

class KuaFoodCameraViewController(private val context: Context, messenger: BinaryMessenger, val id: Int, val params: HashMap<String, Any>) :PlatformView {

    var cameraView: KuaFoodCameraView? = null;

    private var _cameraId: String = "0";

    private val channel: MethodChannel = MethodChannel(messenger, "com.kuafood.kms.android/camera")

    init {
//        channel.setMethodCallHandler(this);
        channel.setMethodCallHandler { call, result -> onCameraMethodCall(call,result) }
        params.entries.forEach {
            Log.i("rex", "初始化接收入参：${it.key} - ${it.value}")

            if (it.key == "cameraId"){
                _cameraId = (it.value ?: "") as String;
            }
        }
    }

    override fun getView(): View? {

        return  initCustomView();
    }

    private fun initCustomView(): View {
        if (cameraView == null){
            var map:Map<String, Any> = mapOf("cameraId" to _cameraId);

            cameraView = KuaFoodCameraView(context,null, map);
        }

        return cameraView as KuaFoodCameraView;

    }

    override fun dispose() {
        Log.i("rex", "flutterCameraView on Dispose")
    }


    private fun onCameraMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when (call.method) {
            "getPlatformVersion" -> {
                result.success("sss");
            }

            "getMessageFromFlutterView" -> {
//                cameraView?.getMessageFromFlutter(call.arguments.toString())
                result.success(true)

            }
            "beginRecordVideo" -> {

                result.success(null)

            }

            "endRecordVideo" -> {

                result.success("path")

            }
            "switchFrontCamera" -> {

                result.success(null)

            }
            "switchBackCamera" -> {

                result.success(null)
            }
//            return FlutterKuafoodCameraPlatform.instance.openFlashLight();
            "openFlashLight" -> {
                //打开
//                cameraView?.cameraSurfaceView?.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH)
                result.success(null);
            }
            "closeFlashLight" -> {
//                cameraView?.cameraSurfaceView?.setFlashMode(Camera.Parameters.FLASH_MODE_ON)
                result.success(null);
            }

            else -> {
//                result.success()
            }
        }
    }


}




